import GameUI from '../scene/GameUI'
import EventManager from '../EventManager'
import { EEventType } from './Enum'

export default class Enemy extends Laya.Script {
    constructor() { super(); }

    public hp: number;
    public level: number;

    onEnable(): void {

    }

    onUpdate() {
        if ((this.owner as Laya.Sprite).y > Laya.stage.height + 80) {
            this.owner.removeSelf();
        }
    }

    onTriggerEnter(other) {
        if (other.label === "cButtle") {
            this.hp -= 1;
            if (this.hp <= 0) {
                this.owner.removeSelf();
                GameUI.it.addScore(this.level);
                /**生成buff */
                if (Math.ceil(Math.random() * 20) === 7) {
                    let owner = this.owner as Laya.Sprite;
                    EventManager.Ins.dispatch(EEventType.CreateBuff, [owner.x, owner.y])
                }
            }
        }
    }

    onDisable(): void {
        Laya.Pool.recover('enemy' + this.level, this.owner)
    }
}